package com.knight.tomcat.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class KnightResponse {

   private OutputStream outputStream;

    public KnightResponse(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void write(String content,String status){

        StringBuilder sb = new StringBuilder();
        if (content==null || content.length()<=0) {
            sb.append("HTTP/1.1 "+status+" OK\n")
                    .append("Content-Type: text/html;\n")
                    .append("\r\n");
        }else{
            sb.append("HTTP/1.1 "+status+" OK\n")
                    .append("Content-Type: text/html;\n")
                    .append("\r\n")
                    .append(content);
        }
        try {
            outputStream.write(sb.toString().getBytes());
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void write(String content){

        write(content,"200");
    }
}
